<?php
declare (strict_types=1);

namespace app\admin\service\finance;

use app\admin\logic\finance\Split as SplitLogic;
use app\common\service\BaseService;
use app\course\logic\base\Course;
use app\user\logic\user\Account as User;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 分成后台管理
 */
class Split extends BaseService
{
    /**
     * @Inject()
     *
     * @var SplitLogic
     */
    protected $splitLogic;

    /**
     * 分成明细
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-08-19
     */
    public function list()
    {
        // 搜索功能
        $map = [];
        Request::has('mhm_id', 'get', true) && $map[] = ['mhm_id', '=', Request::get('mhm_id/d')];
        if (Request::has('user_name', 'get', true)) {
            $user_ids = app(User::class)
                ->where('user_name', 'like', '%' . Request::get('user_name/s') . '%')
                ->column('id');

            $map[] = ['user_id', 'IN', $user_ids];
        }
        if (Request::has('course_title', 'get', true)) {
            $user_ids = app(Course::class)
                ->where('title', 'like', '%' . Request::get('course_title/s') . '%')
                ->column('id');

            $map[] = ['course_id', 'IN', $user_ids];
        }

        $data = $this->splitLogic->getList($map);

        // 定义字段
        $keyList = [
            'user_name'     => ListItem::make()->title('用户'),
            'course_title'  => ListItem::make()->title('课程'),
            'section_title' => ListItem::make()->title('课时'),
            'school_name'   => ListItem::make()->title('课程机构'),
            'payment'       => ListItem::make('custom')->title('支付金额'),
            'platform_sum'  => ListItem::make('custom')->title('平台分成'),
            'school_user'   => ListItem::make()->title('机构管理员'),
            'school_sum'    => ListItem::make('custom')->title('机构分成'),
            // 'mount_user'    => ListItem::make()->title('挂载机构管理员'),
            // 'mount_sum'     => ListItem::make('custom')->title('挂载机构分成'),
            'teacher_user'  => ListItem::make()->title('讲师'),
            'teacher_sum'   => ListItem::make('custom')->title('讲师分成'),
            'info'          => ListItem::make('custom')->title('其他分成信息'),
            // 'share_user'    => ListItem::make()->title('分享者'),
            // 'share_sum'     => ListItem::make('custom')->title('分享者分成'),
            // 'primary_user'              => ListItem::make()->title('一级分销用户'),
            // 'primary_distribution'      => ListItem::make('custom')->title('一级分销分成'),
            // 'secondary_user'            => ListItem::make()->title('二级分销用户'),
            // 'secondary_distribution'    => ListItem::make('custom')->title('二级分销分成'),
            // 'status'        => ListItem::make('custom')->title('生成状态'),
            'create_time'   => ListItem::make()->title('创建时间'),
            // 'update_time'   => ListItem::make()->title('最后更新时间'),
        ];

        $is_platform = request()->is_platform;
        $searchKey   = [
            'mhm_id'       => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'user_name'    => FormItem::make('input')->title(false)->placeholder('用户名')->maxlength(30),
            'course_title' => FormItem::make('input')->title(false)->placeholder('课程')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 用户分成
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-08-19
     */
    public function user()
    {
        // 搜索功能
        $map = [];
        Request::has('mhm_id', 'get', true) && $map[] = ['mhm_id', '=', Request::get('mhm_id/d')];
        if (Request::has('user_name', 'get', true)) {
            $user_ids = app(User::class)
                ->where('user_name', 'like', '%' . Request::get('user_name/s') . '%')
                ->column('id');

            $map[] = ['user_id', 'IN', $user_ids];
        }

        $data = $this->splitLogic->getUserList($map);

        // 定义字段
        $keyList = [
            'user_name'     => ListItem::make()->title('用户名'),
            'school_name'   => ListItem::make()->title('用户机构'),
            'superior_user' => ListItem::make()->title('上级用户'),
            'amount'        => ListItem::make('custom')->title('可用分成'),
            'frozen'        => ListItem::make('custom')->title('冻结分成'),
            // 'school_name'   => ListItem::make()->title('课程机构'),
            'update_time'   => ListItem::make()->title('最后更新时间'),
        ];

        $is_platform = request()->is_platform;
        $searchKey   = [
            'mhm_id'    => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'user_name' => FormItem::make('input')->title(false)->placeholder('用户名')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 用户分成流水
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-08-19
     */
    public function flow($user_id)
    {
        $data = $this->splitLogic->getUserFlow($user_id);

        // 定义字段
        $keyList = [
            'id'          => ListItem::make()->title('ID'),
            'type_text'   => ListItem::make()->title('分成类型'),
            'num'         => ListItem::make('custom')->title('分成金额'),
            'amount'      => ListItem::make('custom')->title('分成后可用金额'),
            'frozen'      => ListItem::make('custom')->title('分成后冻结金额'),
            'note'        => ListItem::make()->title('备注'),
            'status'      => ListItem::make('custom')->title('冻结状态'),
            'create_time' => ListItem::make()->title('分成时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        // $list->searchKey = $searchKey;
        $list->datas = $data;

        return $list->send();
    }

}
